﻿
var page = new lg.page({
    data: {
        classId: 0,
        classSelect: null
    },
    mounted: function () {
        this.GetTreeHtml();
        this.GetClassForSelect();
        this.List();
    },
    methods: {
        GetTreeHtml: function () {
            lg.request({
                url: "/sysapi/GetClassTreeHtml",
                success: function (d) {
                    if (d.code == 0) {
                        $(".lg-tree").html(d.data)
                    }
                }
            })
        },
        GetClassForSelect: function () {
            lg.request({
                url: "/SysApi/GetClassForSelect",
                success: function (d) {
                    if (d.code == 0 && d.data != null) {
                        for (var i in d.data) {
                            var obj = d.data[i];
                            if (obj.Depth > 1) {
                                obj.ClassName = lg.string.PadLeft(obj.ClassName, obj.Depth - 1, '　')
                            }
                        }
                    }
                    page.classSelect = d.data;
                }
            })
        },
        List: function () {
            layui.use(['form', 'table', 'upload'], function () {
                var table = layui.table;
                var form = layui.form;
                page.laytable = table.render({
                    elem: '#ArticleList'
                    , url: '/SysApi/GetArticles'
                    , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        {
                            field: 'rownum', title: '#', width: 60, templet: function (d) {
                                return d.LAY_TABLE_INDEX + 1
                            }
                        },
                        {
                            field: 'Title', title: '文章标题'
                        }
                        , {
                            field: 'ClassName', title: '所属分类', width: 200,
                        }
                        , {
                            field: 'AddTime', title: '发布日期', width: 120, templet: function (d) {
                                return d.AddTime.replace(/(\w.+)T(\w.+)(\.\w.+)?/g, "$1")
                            }
                        }
                        , {
                            field: 'IsTop', title: '置顶', width: 100, align: "center", templet: function (d) {
                                return laySwitch.render(d.IsTop, 'top');
                            }
                        }
                        , {
                            field: 'IsHot', title: '推荐', width: 100, align: "center", templet: function (d) {
                                return laySwitch.render(d.IsHot, 'hot')
                            }
                        }
                        , {
                            field: 'IsCheck', title: '审核', width: 100, align: "center", templet: function (d) {
                                return laySwitch.render(d.IsCheck, 'check')
                            }
                        }
                        , { field: 'SortNumber', title: '权重值', width: 90, align: "center" }
                        , { fixed: 'right', title: '操作', toolbar: '#btTpl', width: 220 }
                    ]],
                });
                //头工具栏事件
                table.on('toolbar(ArticleList)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addArticle':
                            loadTpl({
                                url: "/Lgm/AddArticle/" + page.classId,
                                target: "#floatBody"
                            })
                            return false;
                            break;
                        case 'refresh':
                            page.laytable.reload();
                            break;
                    };
                });
                //监听行工具事件
                table.on('tool(ArticleList)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('真的删除行么', function (index) {
                                obj.del();
                                layer.close(index);
                            });
                            break;
                        case 'edit':
                            loadTpl({
                                url: "/Lgm/EditArticle/" + data.Id,
                                target: "#floatBody"
                            })
                            break;
                        case 'photo':
                            loadTpl({
                                url: "/Lgm/EditArticle/" + data.Id,
                                target: "#floatBody"
                            })
                            break;
                        case 'Extend':

                            break;
                        case 'top':
                            laySwitch.do({
                                obj: obj,
                                key: "IsTop",
                                url: "/SysApi/EditArticle",
                                refresh: true
                            })
                            break;
                        case 'hot':
                            laySwitch.do({
                                obj: obj,
                                key: "IsHot",
                                url: "/SysApi/EditArticle"
                            })
                            break;
                        case 'check':
                            laySwitch.do({
                                obj: obj,
                                key: "IsCheck",
                                url: "/SysApi/EditArticle"
                            })
                            break;
                    }

                });
                form.on('submit(bt_Search)', function (data) {
                    page.laytable.reload({
                        where: data.field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    return false;
                });
                //导航按钮菜单事件注册
                $('.lg-tree').on('click', "li span", function () {
                    console.log("事件触发")
                    $(this).toggleClass("fa-caret-right fa-caret-down");
                    $(this).siblings("ul").toggle();
                });
                $('.lg-tree').on("click", "li a", function (e) {
                    $(".lg-tree a").removeClass("act");
                    $(this).addClass("act");
                    var cid = $(this).data("id");
                    $("#SForm")[0].reset();
                    form.render();
                    page.laytable.reload({
                        where: { "ClassId": cid },
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });

                })

            });
        },
        Add: function () {
            editor.render("editor");
            var form = layui.form;
            var upload = layui.upload
                , laydate = layui.laydate
                , layer = layui.layer;

            if (this.classSelect) {
                lg.select.fill({
                    data: this.classSelect,
                    value: "Id",
                    text: "ClassName",
                    target: "#ClassId",
                    callback: function () {
                        form.render();
                    }
                })
            }
            //拖拽上传
            upload.render({
                elem: '#upImg'
                , url: '/SysApi/SmallImgUpLoad' //改成您自己的上传接口
                , done: function (res) {
                    if (res.state) {
                        layer.msg('上传成功', { icon: 1 });
                        $(this.elem).find(".viewImg").html("<hr><img src='" + res.filePath + "' />");
                        $("#ImageUrl").val(res.filePath);
                    } else {
                        layer.msg(d.msg, { icon: 2 })
                    }
                }
            });
            //提交事件
            form.on("submit(AddArticle)", function (d) {
                lg.request({
                    url: "/SysApi/AddArticle",
                    data: d.field,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("添加成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
        },
        Edit: function (id) {
            var form = layui.form;
            form.render();
            if (page.classSelect) {
                lg.select.fill({
                    data: page.classSelect,
                    value: "Id",
                    text: "ClassName",
                    target: "#ClassId",
                    callback: function () {
                        form.render();
                    }
                })
            }
            lg.request({
                url: "/SysApi/GetArticle/"+id,
                success: function (d) {
                    if (d.code == 0) {
                        if (typeof d.data == "object") {
                            form.val("ArticleEditForm", d.data)
                            $(".viewImg").html("<hr><img src='" + d.data.IconUrl + "' />")
                            editor.render("editor");
                        }
                    }
                }
            })

            form.on("submit(EditArticle)", function (d) {
                lg.request({
                    url: "/SysApi/EditArticle",
                    data: d.field,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("编辑成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
        }
    }
});


